package br.usp.ime.AUN;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;

import br.usp.mac5855.AnalisadorTextual;
import br.usp.mac5855.exception.AnalisadorTextualException;

import com.alag.ci.textanalysis.TagMagnitudeVector;

@Entity
@Indexed
public class Materia implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private long id;
	
	private String codMateria;
	
	@Field(index=Index.TOKENIZED)
	private String titulo;
	
	@Field(index=Index.TOKENIZED)
	@Column(columnDefinition = "MEDIUMTEXT")
	private String corpoHTML;
	
	@Field(index=Index.TOKENIZED)
	@Column(columnDefinition = "MEDIUMTEXT")
	private String corpoTexto;
	
	private double[] similaresProduct;
	
	private long[] similaresId;
	
	@Column(columnDefinition = "MEDIUMTEXT")
	private String[] similaresTitulo;
	
	@Transient
	private TagMagnitudeVector tmv;

	
	public Materia(){
		this.setTitulo("");
		this.setCorpo("");
		setTmv(null);
		similaresProduct = new double[]{0.0,0.0,0.0,0.0,0.0};
		similaresId = new long[]{-1L,-1L,-1L,-1L,-1L};
		similaresTitulo = new String[]{"","","","",""};
	}
	
	public Materia(String titulo, String corpo){
		this.setTitulo(titulo);
		this.setCorpo(corpo);
		System.out.println("Corpo texto "+ this.corpoTexto);
		AnalisadorTextual at = new AnalisadorTextual(); 
		try {
			setTmv(at.analisar(titulo+corpo));
		} catch (AnalisadorTextualException e) {
			setTmv(null);
			e.printStackTrace();
		}
		similaresProduct = new double[]{0.0,0.0,0.0,0.0,0.0};
		similaresId = new long[]{-1L,-1L,-1L,-1L,-1L};
		similaresTitulo = new String[]{"","","","",""};
	}
	
	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}
	
	public String getTitulo() {
		return titulo;
	}
	
	public void setCorpo(String corpo) {
		this.corpoHTML = corpo;
		if (corpo.length()> 34){
			this.corpoTexto=((corpo.substring(34)).replaceAll("<+[A-Z0-9._%+-/\\\"\'= ]+>", " "));
		} else {
			this.corpoTexto=corpo.replaceAll("<+[A-Z0-9._%+-/\\\"\'= ]+>", " ");
		}
	}
	
	public String getCorpo() {
		return corpoHTML;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setCodMateria(String codMateria) {
		this.codMateria = codMateria;
	}

	public String getCodMateria() {
		return codMateria;
	}

	public void setTmv(TagMagnitudeVector tmv) {
		this.tmv = tmv;
	}

	public TagMagnitudeVector getTmv() {
		AnalisadorTextual at = new AnalisadorTextual(); 
		if (tmv == null){
			try {
				setTmv(at.analisar(titulo.replaceAll("<+[A-Z0-9._%+-/\\\"\'= ]+>", " ")+corpoTexto));
			} catch (AnalisadorTextualException e) {
				setTmv(null);
				e.printStackTrace();
			}
		}
		return tmv;
	}

	public boolean verificaEAdicionaSimilar(Materia materia) {
		boolean paraRetornar = false;
		boolean[] menores = new boolean[]{false,false,false,false,false}; 
		if (this!=materia){
			double product = this.getTmv().dotProduct(materia.getTmv());
			for (int i = 0; i < similaresId.length; i++) {
				if (similaresProduct[i] < product){
					menores[i] = true;
				}
			}
			int menor = -1;
			for (int i = 0; i < similaresId.length; i++) {
				if (menores[i]){
					if (menor == -1){
						menor = i;
					} else {
						if (similaresProduct[i]<similaresProduct[menor]){
							menor = i;
						}
					}
				}
			}
			if (menor != -1){
				similaresProduct[menor] = product;
				similaresId[menor] = materia.getId();
				similaresTitulo[menor] = materia.getTitulo();
			}
		}
		return paraRetornar;
	}

	public String imprimeHTML(){
		String paraRetornar = "";
		paraRetornar += "<div>";
		paraRetornar += "  <p>";
		paraRetornar += "    <h3>";
		paraRetornar += "      "+this.getId()+" - "+this.getTitulo();
		paraRetornar += "    </h3>";
		paraRetornar += "    "+this.getCorpo();
		paraRetornar += "  </p>";
		//paraRetornar += "  <br> Sem html <br>";
		//paraRetornar += "    "+this.getCorpoTexto();
		//paraRetornar += "  <br> Sem html <br>";
		for (int i = 0; i < similaresId.length; i++) {
			if (similaresId[i] != -1L){
				paraRetornar += "<a href=\"materia.jsp?id="+similaresId[i]+"\">"+similaresId[i]+" - "+similaresTitulo[i]+"</a> "+similaresProduct[i];
				paraRetornar += "  <br/>";
			}
		}
		AnalisadorTextual at = new AnalisadorTextual();
		paraRetornar +=  at.createTagCloud(this.getTmv());
		paraRetornar +=  "<br><h3> Matriz de radicais </h3>";
		paraRetornar +=  this.getTmv();
		paraRetornar += "</div>";
		return paraRetornar;
	}
/*	
	public boolean equals(Object obj){
		try{
			return this.getId() == ((Materia)obj).getId();
		}catch (Exception e) {
			return false;
		}
	}
	*/

	public String getCorpoTexto() {
		return corpoTexto;
	}
}